package com.china08.yxyapi.api.home.kecheng.cloud;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudCatalog;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudCatalogRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

/****
 * 
 * @annotation :云课程--课程查询
 * @author :yaoguanglin
 */
@CrossOrigin
@RestController
public class KechengCloudLessonSrhController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsCloudLessonRepository yxyLessonsCloudLessonRepository;

    @Autowired
    private YxyLessonsCloudCatalogRepository yxyLessonsCloudCatalogRepository;

    @Autowired
    private YxyLessonsCloudLessonItemRepository yxyLessonsCloudLessonItemRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/lesson/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        Pageable pageable = null;
        if (reqModel.equals("updateDate")) {
            pageable = reqModel.getPageable(Direction.ASC, reqModel.condition);
        } else {
            pageable = reqModel.getPageable(Direction.DESC, reqModel.condition);
        }
        Page<YxyLessonsCloudLesson> page = null;
        List<String> idsList = new ArrayList<String>();
        List<CatalogId> catalogs = reqModel.catalogs;
        Integer level = reqModel.level;
        String keyword = reqModel.keyword;
        String id = reqModel.id;
        Integer jing = reqModel.isJing;
        if (jing == 0) {
            if (StringUtils.isBlank(keyword)) {
                if (level == 1) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId1In(2, 1, idsList,
                                pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId1(2, 1, id,
                                pageable);
                    }

                } else if (level == 2) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId2In(2, 1, idsList,
                                pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId2(2, 1, id,
                                pageable);
                    }
                } else if (level == 3) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId3In(2, 1, idsList,
                                pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId3(2, 1, id,
                                pageable);
                    }
                } else if (level == 4) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId4In(2, 1, idsList,
                                pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId4(2, 1, id,
                                pageable);
                    }
                } else if (level == 5) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId5In(2, 1, idsList,
                                pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId5(2, 1, id,
                                pageable);
                    }
                }

            } else {
                if (level == 1) {

                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId1InAndNameContaining(
                                2, 1, idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId1AndNameContaining(2,
                                1, id, keyword, pageable);
                    }

                } else if (level == 2) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId2InAndNameContaining(
                                2, 1, idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId2AndNameContaining(2,
                                1, id, keyword, pageable);
                    }
                } else if (level == 3) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId3InAndNameContaining(
                                2, 1, idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId3AndNameContaining(2,
                                1, id, keyword, pageable);
                    }
                } else if (level == 4) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId4InAndNameContaining(
                                2, 1, idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId4AndNameContaining(2,
                                1, id, keyword, pageable);
                    }
                } else if (level == 5) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId5InAndNameContaining(
                                2, 1, idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository.findByStatusAndIsVisibleAndCatalogId5AndNameContaining(2,
                                1, id, keyword, pageable);
                    }
                }
            }
        } else {
            // 查询精品
            if (StringUtils.isBlank(keyword)) {
                if (level == 1) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId1In(1, 2, 1, idsList, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId1(1, 2, 1, id, pageable);
                    }

                } else if (level == 2) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId2In(1, 2, 1, idsList, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId2(1, 2, 1, id, pageable);
                    }
                } else if (level == 3) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId3In(1, 2, 1, idsList, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId3(1, 2, 1, id, pageable);
                    }
                } else if (level == 4) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId4In(1, 2, 1, idsList, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId4(1, 2, 1, id, pageable);
                    }
                } else if (level == 5) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId5In(1, 2, 1, idsList, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId5(1, 2, 1, id, pageable);
                    }
                }

            } else {
                if (level == 1) {

                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId1InAndNameContaining(1, 2, 1,
                                        idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId1AndNameContaining(2, 1, 1, id,
                                        keyword, pageable);
                    }

                } else if (level == 2) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId2InAndNameContaining(1, 2, 1,
                                        idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId2AndNameContaining(1, 2, 1, id,
                                        keyword, pageable);
                    }
                } else if (level == 3) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId3InAndNameContaining(1, 2, 1,
                                        idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId3AndNameContaining(1, 2, 1, id,
                                        keyword, pageable);
                    }
                } else if (level == 4) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId4InAndNameContaining(1, 2, 1,
                                        idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId4AndNameContaining(1, 2, 1, id,
                                        keyword, pageable);
                    }
                } else if (level == 5) {
                    if (id.equals("all")) {
                        if (catalogs != null && catalogs.size() > 0) {
                            for (CatalogId catalogId : catalogs) {
                                idsList.add(catalogId.id);
                            }
                        }
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId5InAndNameContaining(1, 2, 1,
                                        idsList, keyword, pageable);
                    } else {
                        page = yxyLessonsCloudLessonRepository
                                .findByIsJingpinLessonAndStatusAndIsVisibleAndCatalogId5AndNameContaining(1, 2, 1, id,
                                        keyword, pageable);
                    }
                }
            }
        }
        List<RespModel> respModels = new ArrayList<KechengCloudLessonSrhController.RespModel>();
        if (page.getContent() != null && page.getContent().size() > 0) {

            for (YxyLessonsCloudLesson yxyLessonsCloudLesson : page) {
                RespModel model = new RespModel();
                YxyLessonsCloudCatalog yxyLessonsCloudCatalog = null;
                // 获取最末节的目录名称
                if (StringUtils.isNotBlank(yxyLessonsCloudLesson.getCatalogId5())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository
                            .findById(yxyLessonsCloudLesson.getCatalogId5());
                } else if (StringUtils.isNotBlank(yxyLessonsCloudLesson.getCatalogId4())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository
                            .findById(yxyLessonsCloudLesson.getCatalogId4());
                } else if (StringUtils.isNotBlank(yxyLessonsCloudLesson.getCatalogId3())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository
                            .findById(yxyLessonsCloudLesson.getCatalogId3());
                } else if (StringUtils.isNotBlank(yxyLessonsCloudLesson.getCatalogId2())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository
                            .findById(yxyLessonsCloudLesson.getCatalogId2());
                } else if (StringUtils.isNotBlank(yxyLessonsCloudLesson.getCatalogId1())) {
                    yxyLessonsCloudCatalog = yxyLessonsCloudCatalogRepository
                            .findById(yxyLessonsCloudLesson.getCatalogId1());
                }
                model.catalogName = yxyLessonsCloudCatalog.getName();
                model.count = yxyLessonsCloudLessonItemRepository
                        .countByLessonIdAndStatus(yxyLessonsCloudLesson.getId(), 2);
                model.cover = yxyLessonsCloudLesson.getCover();
                model.ep = yxyLessonsCloudLesson.getEp();
                model.EpTimes = yxyLessonsCloudLesson.getEpTimes();
                model.lessonId = yxyLessonsCloudLesson.getId();
                model.name = yxyLessonsCloudLesson.getName();
                model.vv = yxyLessonsCloudLesson.getVv();
                model.isJing = yxyLessonsCloudLesson.getIsJingpinLesson();
                model.clickPv = yxyLessonsCloudLesson.getClickPv() == null ? 0 : yxyLessonsCloudLesson.getClickPv();
                respModels.add(model);
            }

        }
        Page<RespModel> respPage = new PageImpl<>(respModels, pageable, page.getTotalElements());
        return respPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 目录ID
        public String id;
        // 目录等级
        public Integer level;
        // 关键字
        public String keyword;
        // 筛选条件
        public String condition;
        // 是否精品 0 全部 1 是
        public Integer isJing;
        // 目录ID
        public List<CatalogId> catalogs;
    }

    protected static class CatalogId {
        // 目录名称
        public String name;
        // 目录ID
        public String id;
        // 父级目录ID
        public String pid;
        // 目录等级
        public Integer level;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 课程名称
        public String name;
        // 课程评分
        public Float ep;
        // 评价人数
        public Integer EpTimes;
        // 学习人数
        public Integer vv;
        // 点击次数
        public Integer clickPv;
        // 课程Id
        public String lessonId;
        // 所属目录（最后一级目录名称）
        public String catalogName;
        // 是否是精品 0 不是 1 是
        public Integer isJing;
    }
}
